from sqlalchemy import *

import os, sys
APPLICATION_INSTALL_DIR = os.environ.get('SIMPLEAUTH_INSTALL')
if not APPLICATION_INSTALL_DIR:
    raise EnvironmentError("Must Set SIMPLEAUTH_INSTALL in the environment!")
sys.path.append(os.path.join(APPLICATION_INSTALL_DIR, "lib%spython" % os.sep))
from simple_auth.envbuilder import EnvironmentBuilder

env = EnvironmentBuilder()

engine = create_engine(env.simple_dburi, pool_recycle=3600)
metadata = BoundMetaData(engine)

users = Table('users', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(64), nullable=False, unique=True),
    Column('pass', String(64), nullable=False, key="password"),
)

services = Table('services', metadata,
    Column('id', Integer, primary_key=True),
    Column('name', String(64), nullable=False, unique=True),
)

roles = Table('roles', metadata, 
    Column('id', Integer, primary_key=True),
    Column('name', String(64), nullable=False, unique=True),
)

roles_services = Table('roles_services', metadata,
    Column('role_id', Integer, ForeignKey('roles.id'), primary_key=True),
    Column('service_id', Integer, ForeignKey('services.id'), primary_key=True),
)

roles_users = Table('roles_users', metadata,
    Column('role_id', Integer, ForeignKey('roles.id'), primary_key=True),
    Column('user_id', Integer, ForeignKey('users.id'), primary_key=True),
)

resources = Table('resources', metadata, 
    Column('id', Integer, primary_key=True),
    Column('name', String(64), nullable=False, unique=True),
)

resource_users = Table('resource_users', metadata, 
    Column('id', Integer, primary_key=True),
    Column('resource_id', Integer, ForeignKey('resources.id'), nullable=False),
    Column('name', String(64), nullable=False),
    Column('pass', String(64), nullable=False, key="password"),
)

resource_users_roles = Table('resource_users_roles', metadata, 
    Column('resource_user_id', Integer, ForeignKey('resource_users.id'), primary_key=True),
    Column('role_id', Integer, ForeignKey('roles.id'), primary_key=True),
)